/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.xb.ctx;

import axescon.xacml.model.ctx.*;
import axescon.xacml.model.ctx.impl.*;
import axescon.xacml.model.shared.Decision;
import axescon.xacml.api.ResponseFactory;
import x0ContextSchemaOs.oasisNamesTcXacml2.*;

import java.io.Reader;

/**
 * @author argyn
 *         Date: Sep 19, 2005
 *         Responsibilities: load RequestFactory from XML and save it to XML
 *         using XMLBeans
 */
public class XBResponseFactory implements ResponseFactory {
    public Response getResponse(Reader r) throws Exception {
        return getResponse(ResponseDocument.Factory.parse(r).getResponse());
    }

    private Response getResponse(ResponseType xbrt) throws Exception {
        ResponseImpl ret = new ResponseImpl();
        ret.setResultArray(getResultArray(xbrt));

        return ret;
    }

    private Result[] getResultArray(ResponseType xbrt) throws Exception {
        if (xbrt.sizeOfResultArray() == 0) return null;

        Result[] ret = new Result[xbrt.sizeOfResultArray()];
        for (int i = 0; i < xbrt.sizeOfResultArray(); i++) {
            ret[i] = getResult(xbrt.getResultArray(i));
        }
        return ret;
    }


    public Result getResult(ResultType xbrt) throws Exception {
        if (xbrt == null) return null;

        Result ret = new ResultImpl();
        ret.setResourceId(xbrt.getResourceId());
        ret.setDecision(Decision.forString(xbrt.getDecision().toString()));
        if (xbrt.isSetObligations())
            ret.setObligationArray(getXbObligationFactory()
                    .getObligationArray(xbrt.getObligations().getObligationArray(), null));
        ret.setStatus(getStatus(xbrt.getStatus()));
        return ret;
    }

    public Status getStatus(StatusType xbet) throws Exception {
        if (xbet == null) return null;

        Status ret = new StatusImpl();
        ret.setStatusCode(getStatusCode(xbet.getStatusCode()));
        ret.setStatusDetail(getStatusDetail(xbet.getStatusDetail()));
        ret.setStatusMessage(xbet.getStatusMessage());
        return ret;
    }

    public StatusCode getStatusCode(StatusCodeType xbrt) throws Exception {
        if (xbrt == null) return null;

        StatusCode ret = new StatusCodeImpl();
        ret.setStatusCode(getStatusCode(xbrt.getStatusCode()));
        ret.setValue(xbrt.getValue());
        return ret;
    }

    public StatusDetail getStatusDetail(StatusDetailType xbat) throws Exception {
        if (xbat == null) return null;

        StatusDetail ret = new StatusDetailImpl();
        //TODO: implementation complete
        return ret;
    }


    private XBObligationFactory xbObligationFactory;

    public XBObligationFactory getXbObligationFactory() {
        return xbObligationFactory;
    }

    public void setXbObligationFactory(XBObligationFactory xbObligationFactory) {
        this.xbObligationFactory = xbObligationFactory;
    }

    public XBResponseFactory(XBObligationFactory xbObligationFactory) {
        this.xbObligationFactory = xbObligationFactory;
    }

    public XBResponseFactory(boolean setDefaults) {
        if (setDefaults) setDefaults();
    }

    public XBResponseFactory() {
        this(false);
    }

    public void setDefaults() {
        setXbObligationFactory(new XBObligationFactory());
    }


}
